<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>canvasApi 的方法 | 孙加俊的个人博客</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/sjj-blog/favicon.ico">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/sjj-blog/assets/css/0.styles.a5cb6817.css" as="style"><link rel="preload" href="/sjj-blog/assets/js/app.ab5bad9b.js" as="script"><link rel="preload" href="/sjj-blog/assets/js/3.136f62ef.js" as="script"><link rel="preload" href="/sjj-blog/assets/js/1.a27fba8e.js" as="script"><link rel="preload" href="/sjj-blog/assets/js/13.ace41b19.js" as="script"><link rel="prefetch" href="/sjj-blog/assets/js/10.9271e5b8.js"><link rel="prefetch" href="/sjj-blog/assets/js/11.67b9ea44.js"><link rel="prefetch" href="/sjj-blog/assets/js/12.f69e6353.js"><link rel="prefetch" href="/sjj-blog/assets/js/14.78670b1e.js"><link rel="prefetch" href="/sjj-blog/assets/js/15.b0aaec82.js"><link rel="prefetch" href="/sjj-blog/assets/js/16.f0d479dc.js"><link rel="prefetch" href="/sjj-blog/assets/js/17.1bacbab3.js"><link rel="prefetch" href="/sjj-blog/assets/js/18.2237a65f.js"><link rel="prefetch" href="/sjj-blog/assets/js/19.7e0f4bf8.js"><link rel="prefetch" href="/sjj-blog/assets/js/20.5c098df8.js"><link rel="prefetch" href="/sjj-blog/assets/js/21.45bd5038.js"><link rel="prefetch" href="/sjj-blog/assets/js/4.cc582e42.js"><link rel="prefetch" href="/sjj-blog/assets/js/5.9c9443fe.js"><link rel="prefetch" href="/sjj-blog/assets/js/6.370fb6dd.js"><link rel="prefetch" href="/sjj-blog/assets/js/7.e5d26899.js"><link rel="prefetch" href="/sjj-blog/assets/js/8.f6b78670.js"><link rel="prefetch" href="/sjj-blog/assets/js/9.d915a2b8.js">
    <link rel="stylesheet" href="/sjj-blog/assets/css/0.styles.a5cb6817.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-1aefc0b4><div data-v-1aefc0b4><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1aefc0b4 data-v-1aefc0b4><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>孙加俊的个人博客</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2></p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>孙加俊</span>
            
          <span data-v-25ba6db2>2017 - </span>
          2023
        </a></span></div></div> <div class="hide" data-v-1aefc0b4><header class="navbar" data-v-1aefc0b4><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/sjj-blog/" class="home-link router-link-active"><img src="/sjj-blog/logo.png" alt="孙加俊的个人博客" class="logo"> <span class="site-name">孙加俊的个人博客</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/sjj-blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      技术栈
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/sjj-blog/categories/规范/" class="nav-link"><i class="undefined"></i>
  规范
</a></li><li class="dropdown-item"><!----> <a href="/sjj-blog/categories/原生JS/" class="nav-link"><i class="undefined"></i>
  原生JS
</a></li></ul></div></div><div class="nav-item"><a href="/sjj-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/sjj-blog/docs/theme-reco/" class="nav-link"><i class="undefined"></i>
  vuepress-reco
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系我
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/sir-jun" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1aefc0b4></div> <aside class="sidebar" data-v-1aefc0b4><div class="personal-info-wrapper" data-v-39576ba9 data-v-1aefc0b4><img src="/sjj-blog/avatar.png" alt="author-avatar" class="personal-img" data-v-39576ba9> <h3 class="name" data-v-39576ba9>
    孙加俊
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>11</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>3</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/sjj-blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      技术栈
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/sjj-blog/categories/规范/" class="nav-link"><i class="undefined"></i>
  规范
</a></li><li class="dropdown-item"><!----> <a href="/sjj-blog/categories/原生JS/" class="nav-link"><i class="undefined"></i>
  原生JS
</a></li></ul></div></div><div class="nav-item"><a href="/sjj-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/sjj-blog/docs/theme-reco/" class="nav-link"><i class="undefined"></i>
  vuepress-reco
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系我
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/sir-jun" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>canvasApi 的方法</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>孙加俊</span>
            
          <span data-v-25ba6db2>2017 - </span>
          2023
        </a></span></div></div> <div data-v-1aefc0b4><main class="page"><section><div class="page-title"><h1 class="title">canvasApi 的方法</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>孙加俊</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>5/11/2022</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>JS</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="canvas-api-的使用"><a href="#canvas-api-的使用" class="header-anchor">#</a> Canvas APi 的使用</h1> <h2 id="线条样式和基于状态绘制"><a href="#线条样式和基于状态绘制" class="header-anchor">#</a> 线条样式和基于状态绘制</h2> <p>canvas 使基于状态的绘制，因此需要beginPath 和 closePath的辅助 必须在 moveTo, lineTo等动作做完后再 stroke() 需要注意如果在(0, 0)点绘制时，Canvas 路径在 x 轴和 y 轴方向上都绘制到了起点的外侧</p> <div class="language-js extra-class"><pre class="language-js"><code>window<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">var</span> canvas <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">&quot;canvas&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    canvas<span class="token punctuation">.</span>width <span class="token operator">=</span> <span class="token number">800</span><span class="token punctuation">;</span>
    canvas<span class="token punctuation">.</span>height <span class="token operator">=</span> <span class="token number">600</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> context <span class="token operator">=</span> canvas<span class="token punctuation">.</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token string">&quot;2d&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    context<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    context<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span><span class="token number">300</span><span class="token punctuation">,</span><span class="token number">300</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    context<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">'yellow'</span>
    context<span class="token punctuation">.</span>lineWidth <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
    context<span class="token punctuation">.</span>strokeStyle <span class="token operator">=</span> <span class="token string">&quot;#AA394C&quot;</span><span class="token punctuation">;</span>
    context<span class="token punctuation">.</span><span class="token function">fill</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    context<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="快速生成矩形"><a href="#快速生成矩形" class="header-anchor">#</a> 快速生成矩形</h2> <p>convas2d提供了绘制矩形的两个类: fillRect()和strokeRect()</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> canvasRef <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">[</span><span class="token string">'canvas'</span><span class="token punctuation">]</span>
  <span class="token keyword">const</span> context <span class="token operator">=</span> canvasRef<span class="token punctuation">.</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token string">&quot;2d&quot;</span><span class="token punctuation">)</span>
  canvasRef<span class="token punctuation">.</span>width <span class="token operator">=</span> <span class="token number">800</span>
  canvasRef<span class="token punctuation">.</span>height <span class="token operator">=</span> <span class="token number">600</span>
  <span class="token keyword">function</span> <span class="token function">drawBlackRect</span><span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span> x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> width<span class="token punctuation">,</span> height</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    ctx<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    ctx<span class="token punctuation">.</span><span class="token function">rect</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> width<span class="token punctuation">,</span> height<span class="token punctuation">)</span>
    ctx<span class="token punctuation">.</span>lineWidth <span class="token operator">=</span> <span class="token number">5</span>
    ctx<span class="token punctuation">.</span>strokeStyle <span class="token operator">=</span> <span class="token string">&quot;black&quot;</span><span class="token punctuation">;</span>
    ctx<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">function</span> <span class="token function">drawWhiteRect</span><span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span> x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> width<span class="token punctuation">,</span> height</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    ctx<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    ctx<span class="token punctuation">.</span><span class="token function">rect</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> width<span class="token punctuation">,</span> height<span class="token punctuation">)</span>
    ctx<span class="token punctuation">.</span>lineWidth <span class="token operator">=</span> <span class="token number">5</span>
    ctx<span class="token punctuation">.</span>strokeStyle <span class="token operator">=</span> <span class="token string">&quot;#fff&quot;</span><span class="token punctuation">;</span>
    ctx<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  context<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  context<span class="token punctuation">.</span><span class="token function">rect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">800</span><span class="token punctuation">,</span> <span class="token number">600</span><span class="token punctuation">)</span>
  context<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">&quot;#aa9033&quot;</span>
  context<span class="token punctuation">.</span><span class="token function">fill</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

  context<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">20</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">drawBlackRect</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> <span class="token number">200</span> <span class="token operator">+</span> <span class="token number">10</span> <span class="token operator">*</span> i<span class="token punctuation">,</span> <span class="token number">100</span> <span class="token operator">+</span> <span class="token number">10</span> <span class="token operator">*</span> i<span class="token punctuation">,</span> <span class="token number">400</span> <span class="token operator">-</span> <span class="token number">20</span> <span class="token operator">*</span> i<span class="token punctuation">,</span> <span class="token number">400</span> <span class="token operator">-</span> <span class="token number">20</span> <span class="token operator">*</span> i<span class="token punctuation">)</span>
    <span class="token function">drawWhiteRect</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> <span class="token number">205</span> <span class="token operator">+</span> <span class="token number">10</span> <span class="token operator">*</span> i<span class="token punctuation">,</span> <span class="token number">105</span> <span class="token operator">+</span> <span class="token number">10</span> <span class="token operator">*</span> i<span class="token punctuation">,</span> <span class="token number">390</span> <span class="token operator">-</span> <span class="token number">20</span> <span class="token operator">*</span> i<span class="token punctuation">,</span> <span class="token number">390</span> <span class="token operator">-</span> <span class="token number">20</span> <span class="token operator">*</span> i<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
</code></pre></div><p>显示结果: <img src="https://guantaocc.github.io/assets/img/canvas-rect.5d815a53.png" alt="canvas矩形"></p> <h2 id="线条属性"><a href="#线条属性" class="header-anchor">#</a> 线条属性</h2> <p>线条的属性共有以下四个：</p> <ol><li>lineCap属性 lineCap 定义上下文中线的端点，可以有以下 3 个值。</li></ol> <p>butt：默认值，端点是垂直于线段边缘的平直边缘。 round：端点是在线段边缘处以线宽为直径的半圆。 square：端点是在选段边缘处以线宽为长、以一半线宽为宽的矩形。 2. lineJoin属性 lineJoin 定义两条线相交产生的拐角，可将其称为连接。在连接处创建一个填充三角形，可以使用 lineJoin 设置它的基本属性。</p> <p>miter：默认值，在连接处边缘延长相接。miterLimit 是角长和线宽所允许的最大比例(默认是 10)。 bevel：连接处是一个对角线斜角。 round：连接处是一个圆。 3. 线宽 lineWidth 定义线的宽度(默认值为 1.0)。</p> <p>4、笔触样式 strokeStyle 定义线和形状边框的颜色和样式。</p> <p>线条的帽子(端点)</p> <div class="language-js extra-class"><pre class="language-js"><code>context<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span><span class="token number">700</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span>lineCap <span class="token operator">=</span> <span class="token string">&quot;round&quot;</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>线条的连接</p> <div class="language-js extra-class"><pre class="language-js"><code>context<span class="token punctuation">.</span>lineWidth <span class="token operator">=</span> <span class="token number">50</span>
context<span class="token punctuation">.</span>stokeStyle <span class="token operator">=</span> <span class="token string">'#1baaaa'</span>
context<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
context<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span>
context<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span><span class="token number">700</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span>
context<span class="token punctuation">.</span>lineJoin <span class="token operator">=</span> <span class="token string">&quot;miter&quot;</span><span class="token punctuation">;</span>
<span class="token comment">// 当为miter时, miterLimit属性</span>
context<span class="token punctuation">.</span>miterLimit <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span>lineWidth <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span>strokeStyle <span class="token operator">=</span> <span class="token string">&quot;red&quot;</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span>lineCap <span class="token operator">=</span> <span class="token string">&quot;round&quot;</span>
context<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div><h2 id="填充色和渐变"><a href="#填充色和渐变" class="header-anchor">#</a> 填充色和渐变</h2> <p>在画布上创建渐变填充有两个基本选项：线性或径向。线性渐变创建一个水平、垂直或者对角线的填充图案。径向渐变自中心点创建一个放射状填充。填充渐变形状分为三步：添加渐变线，为渐变线添加关键色，应用渐变。</p> <ol><li>添加渐变线：</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> grd <span class="token operator">=</span> context<span class="token punctuation">.</span><span class="token function">createLinearGradient</span><span class="token punctuation">(</span>xstart<span class="token punctuation">,</span>ystart<span class="token punctuation">,</span>xend<span class="token punctuation">,</span>yend<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>为渐变线添加关键色</li></ol> <div class="language-js extra-class"><pre class="language-js"><code>grd<span class="token punctuation">.</span><span class="token function">addColorStop</span><span class="token punctuation">(</span>stop<span class="token punctuation">,</span>color<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>这里的stop传递的是 0 ~ 1 的浮点数，代表断点到(xstart,ystart)的距离占整个渐变色长度是比例。</p> <ol start="3"><li>应用渐变：</li></ol> <div class="language-js extra-class"><pre class="language-js"><code>context<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> grd<span class="token punctuation">;</span>
context<span class="token punctuation">.</span>strokeStyle <span class="token operator">=</span> grd<span class="token punctuation">;</span> 
</code></pre></div><p>线性渐变</p> <div class="language-js extra-class"><pre class="language-js"><code>context<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">rect</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">,</span> <span class="token number">600</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> grd <span class="token operator">=</span> context<span class="token punctuation">.</span><span class="token function">createLinearGradient</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
grd<span class="token punctuation">.</span><span class="token function">addColorStop</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">&quot;black&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
grd<span class="token punctuation">.</span><span class="token function">addColorStop</span><span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">,</span> <span class="token string">&quot;white&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
grd<span class="token punctuation">.</span><span class="token function">addColorStop</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">&quot;black&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> grd<span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">fill</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>线性渐变结果: ![canvas矩形](./images/xian-xing-jian-bian.png 400x300)</p> <p>径向渐变：由中间向外发散</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// x0, y0, r0 分别为圆的圆心和半径</span>
<span class="token keyword">var</span> grd <span class="token operator">=</span> context<span class="token punctuation">.</span><span class="token function">createRadialGradient</span><span class="token punctuation">(</span><span class="token number">400</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">//添加颜色断点</span>
grd<span class="token punctuation">.</span><span class="token function">addColorStop</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">&quot;yellow&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
grd<span class="token punctuation">.</span><span class="token function">addColorStop</span><span class="token punctuation">(</span><span class="token number">0.01</span><span class="token punctuation">,</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
grd<span class="token punctuation">.</span><span class="token function">addColorStop</span><span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">,</span> <span class="token string">&quot;black&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
grd<span class="token punctuation">.</span><span class="token function">addColorStop</span><span class="token punctuation">(</span><span class="token number">0.75</span><span class="token punctuation">,</span> <span class="token string">&quot;White&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
grd<span class="token punctuation">.</span><span class="token function">addColorStop</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">&quot;Gray&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">//应用渐变</span>
context<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> grd<span class="token punctuation">;</span>

context<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">600</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>径向渐变结果: ![canvas矩形](./images/jing-xiang-jian-bian.png 400x300)</p> <h2 id="填充图案"><a href="#填充图案" class="header-anchor">#</a> 填充图案</h2> <p>createPattern(img,repeat-style)，第一个是Image对象实例(CanvasImageSource对象)，第二个参数是String类型，表示在形状中如何显示repeat图案</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> img <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Image</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
img<span class="token punctuation">.</span>src <span class="token operator">=</span> <span class="token string">'https://mdn.mozillademos.org/files/222/Canvas_createpattern.png'</span><span class="token punctuation">;</span>
img<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> pattern <span class="token operator">=</span> ctx<span class="token punctuation">.</span><span class="token function">createPattern</span><span class="token punctuation">(</span>img<span class="token punctuation">,</span> <span class="token string">'repeat'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  ctx<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> pattern<span class="token punctuation">;</span>
  ctx<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="绘制复杂路径图形"><a href="#绘制复杂路径图形" class="header-anchor">#</a> 绘制复杂路径图形</h2> <p>根据不同的一些算法曲线绘制复杂的图形，如三角函数，贝塞尔曲线等</p> <ol><li>arc()圆弧 context.arc(x,y,radius,startAngle,endAngle,anticlockwise)</li></ol> <ul><li>画一个半圆</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">drawArt</span><span class="token punctuation">(</span><span class="token parameter">ctx<span class="token punctuation">,</span> x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> radius</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  ctx<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  ctx<span class="token punctuation">.</span><span class="token function">arc</span><span class="token punctuation">(</span>x <span class="token operator">+</span> radius<span class="token punctuation">,</span> y <span class="token operator">+</span> radius<span class="token punctuation">,</span> radius<span class="token punctuation">,</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span><span class="token punctuation">,</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">*</span> <span class="token number">3</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span>
  ctx<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">&quot;#000&quot;</span>
  ctx<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li>画一个圆形的头像并添加边框</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> img <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Image</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
img<span class="token punctuation">.</span>src <span class="token operator">=</span> <span class="token string">'https://mdn.mozillademos.org/files/222/Canvas_createpattern.png'</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span>lineWidth <span class="token operator">=</span> <span class="token number">2</span>
ctx<span class="token punctuation">.</span>strokeStyle <span class="token operator">=</span> <span class="token string">'red'</span>
ctx<span class="token punctuation">.</span><span class="token function">arc</span><span class="token punctuation">(</span><span class="token number">25</span><span class="token punctuation">,</span> <span class="token number">25</span><span class="token punctuation">,</span> <span class="token number">25</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span><span class="token function">clip</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span><span class="token function">closePath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
img<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  ctx<span class="token punctuation">.</span><span class="token function">drawImage</span><span class="token punctuation">(</span>img<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span>
  ctx<span class="token punctuation">.</span><span class="token function">restore</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>arcTo 切点绘制弧线</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 使用切点绘制弧线</span>
ctx<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span>
<span class="token comment">// 使用上面moveTo作为起点，因此 200,200一定在圆弧上</span>
ctx<span class="token punctuation">.</span><span class="token function">arcTo</span><span class="token punctuation">(</span><span class="token number">600</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">600</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span>lineWidth <span class="token operator">=</span> <span class="token number">5</span>
ctx<span class="token punctuation">.</span>strokeStyle <span class="token operator">=</span> <span class="token string">&quot;red&quot;</span>
ctx<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 绘制微信对话框</span>
ctx<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">180</span><span class="token punctuation">)</span>
<span class="token comment">// r = 20</span>
ctx<span class="token punctuation">.</span><span class="token function">arcTo</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">120</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span><span class="token function">arcTo</span><span class="token punctuation">(</span><span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">120</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span><span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">140</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span><span class="token number">320</span><span class="token punctuation">,</span> <span class="token number">150</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span><span class="token function">lineTo</span><span class="token punctuation">(</span><span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">160</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span><span class="token function">arcTo</span><span class="token punctuation">(</span><span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">280</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span><span class="token function">arcTo</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">180</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">'#9eea6a'</span>
ctx<span class="token punctuation">.</span><span class="token function">fill</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div><p>微信气泡框结果: <img src="" alt="微信气泡框"></p> <h2 id="贝塞尔曲线"><a href="#贝塞尔曲线" class="header-anchor">#</a> 贝塞尔曲线</h2> <ul><li>二次贝塞尔曲线 context.quadraticCurveTo(cpx,cpy,x,y);</li></ul> <p>贝塞尔曲线在线转换工具</p> <p>贝塞尔曲线 <a href="http://tinyurl.com/html5quadratic" target="_blank" rel="noopener noreferrer">在线转换工具 (opens new window)<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> context <span class="token operator">=</span> canvas<span class="token punctuation">.</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token string">&quot;2d&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">&quot;#FFF&quot;</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">800</span><span class="token punctuation">,</span><span class="token number">600</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

context<span class="token punctuation">.</span>lineWidth <span class="token operator">=</span> <span class="token number">6</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span>strokeStyle <span class="token operator">=</span> <span class="token string">&quot;#333&quot;</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span><span class="token number">60</span><span class="token punctuation">,</span> <span class="token number">337</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">quadraticCurveTo</span><span class="token punctuation">(</span><span class="token number">256</span><span class="token punctuation">,</span> <span class="token number">43</span><span class="token punctuation">,</span> <span class="token number">458</span><span class="token punctuation">,</span> <span class="token number">336</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ul><li>三次贝塞尔曲线: context.bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y);</li></ul> <h2 id="保存状态"><a href="#保存状态" class="header-anchor">#</a> 保存状态</h2> <p>save: 保存之前绘制的状态 restore: 恢复之前的状态并继续绘制</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> ctx <span class="token operator">=</span> canvas<span class="token punctuation">.</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token string">&quot;2d&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

ctx<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 保存默认的状态</span>

ctx<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

ctx<span class="token punctuation">.</span><span class="token function">restore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 还原到上次保存的默认状态</span>
ctx<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">150</span><span class="token punctuation">,</span> <span class="token number">75</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="平移变换"><a href="#平移变换" class="header-anchor">#</a> 平移变换</h2> <p>canvas变换跟 css变换api相似</p> <p>注意涉及到坐标系原点的操作最好 save 之后再 restore使坐标系恢复到原点</p> <ul><li>平移变换: translate(x, y)</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">&quot;#00AAAA&quot;</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 在平移之前保存状态</span>
ctx<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">&quot;red&quot;</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">translate</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 平移坐标系之后将坐标系释放到(0, 0)原点</span>
ctx<span class="token punctuation">.</span><span class="token function">restore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ul><li>旋转变换: rotate(deg)</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token number">12</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  context<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  context<span class="token punctuation">.</span><span class="token function">translate</span><span class="token punctuation">(</span><span class="token number">70</span> <span class="token operator">+</span> i <span class="token operator">*</span> <span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// 旋转12次后为 2PI, 则回到原来的状态</span>
  context<span class="token punctuation">.</span><span class="token function">rotate</span><span class="token punctuation">(</span>i <span class="token operator">*</span> <span class="token number">30</span> <span class="token operator">*</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">/</span> <span class="token number">180</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  context<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">&quot;red&quot;</span><span class="token punctuation">;</span>
  context<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  context<span class="token punctuation">.</span><span class="token function">restore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li>缩放变换: scale()</li></ul> <p>缩放时，图像左上角坐标的位置也会对应缩放。 缩放时，图像线条的粗细也会对应缩放。</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
ctx<span class="token punctuation">.</span><span class="token function">scale</span><span class="token punctuation">(</span><span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">'red'</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ctx<span class="token punctuation">.</span><span class="token function">restore</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div><h2 id="矩阵变换"><a href="#矩阵变换" class="header-anchor">#</a> 矩阵变换</h2> <p>CanvasRenderingContext2D.transform() 是 Canvas 2D API 使用矩阵多次叠加当前变换的方法，矩阵由方法的参数进行描述。你可以缩放、旋转、移动和倾斜上下文。</p> <p>使用context.transform (1,0,0,1,dx,dy)代替context.translate(dx,dy) 使用context.transform(sx,0,0,sy,0,0)代替context.scale(sx, sy) 使用context.transform(0,b,c,0,0,0)来实现倾斜效果(最实用)。</p> <ul><li>transform</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>context<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//平移至(300,200)</span>
context<span class="token punctuation">.</span><span class="token function">transform</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//水平方向放大2倍，垂直方向放大1.5倍</span>
context<span class="token punctuation">.</span><span class="token function">transform</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1.5</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//水平方向向右倾斜宽度10%的距离，垂直方向向上倾斜高度10%的距离</span>
context<span class="token punctuation">.</span><span class="token function">transform</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">0.1</span><span class="token punctuation">,</span> <span class="token number">0.1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">strokeRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">restore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ul><li>setTransform setTransform()不会相对于其他变换来发生行为,只会先对于原始图形变换</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>context<span class="token punctuation">.</span>fillStyle<span class="token operator">=</span><span class="token string">&quot;yellow&quot;</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token number">250</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">)</span>
context<span class="token punctuation">.</span><span class="token function">setTransform</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">0.5</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">0.5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">30</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span>fillStyle<span class="token operator">=</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token number">250</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="文本显示与渲染"><a href="#文本显示与渲染" class="header-anchor">#</a> 文本显示与渲染</h2> <table><thead><tr><th>属性</th> <th>描述</th></tr></thead> <tbody><tr><td><code>font</code></td> <td>设置或返回文本内容的当前字体属性</td></tr> <tr><td><code>textAlign</code></td> <td>设置或返回文本内容的当前对齐方式</td></tr> <tr><td><code>textBaseline</code></td> <td>设置或返回在绘制文本时使用的当前文本基线</td></tr></tbody></table> <table><thead><tr><th>方法</th> <th>描述</th></tr></thead> <tbody><tr><td><code>fillText()</code></td> <td>在画布上绘制“被填充的”文本</td></tr> <tr><td><code>strokeText()</code></td> <td>在画布上绘制文本（无填充）</td></tr> <tr><td><code>measureText()</code></td> <td>返回包含指定文本对象的宽度</td></tr></tbody></table> <ol><li>显示一段文本</li></ol> <div class="language-js extra-class"><pre class="language-js"><code>context<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">'#f1f1f1'</span>
context<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">)</span>
<span class="token comment">// 1. 设置字体</span>
context<span class="token punctuation">.</span>font <span class="token operator">=</span> <span class="token string">&quot;20px sans-serif&quot;</span>
<span class="token comment">// 2. 设置字体颜色</span>
context<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">&quot;#000&quot;</span>
<span class="token comment">// 3. 显示字体</span>
context<span class="token punctuation">.</span><span class="token function">fillText</span><span class="token punctuation">(</span><span class="token string">&quot;文本&quot;</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token number">8</span> 
</code></pre></div><ol start="2"><li>文本测量</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> txt <span class="token operator">=</span> <span class="token string">&quot;mesureText width&quot;</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">fillText</span><span class="token punctuation">(</span><span class="token string">&quot;width:&quot;</span> <span class="token operator">+</span> context<span class="token punctuation">.</span><span class="token function">measureText</span><span class="token punctuation">(</span>txt<span class="token punctuation">)</span><span class="token punctuation">.</span>width<span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="阴影效果"><a href="#阴影效果" class="header-anchor">#</a> 阴影效果</h2> <p>context.shadowColor：阴影颜色。 context.shadowOffsetX：阴影x轴位移。正值向右，负值向左。 context.shadowOffsetY：阴影y轴位移。正值向下，负值向上。 context.shadowBlur：阴影模糊滤镜。数据越大，扩散程度越大。</p> <h2 id="全局透明"><a href="#全局透明" class="header-anchor">#</a> 全局透明</h2> <p>context.globalAlpha = (0 - 1的透明度);</p> <div class="language-js extra-class"><pre class="language-js"><code>context<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
context<span class="token punctuation">.</span>globalAlpha <span class="token operator">=</span> <span class="token number">0.5</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span>shadowColor <span class="token operator">=</span> <span class="token string">&quot;red&quot;</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span>shadowOffsetX <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span>shadowOffsetY <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span>shadowBlur <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span>

<span class="token comment">// 如果不 restore的话，下面的 rect也会带有阴影</span>
context<span class="token punctuation">.</span><span class="token function">restore</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

context<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span>
</code></pre></div><h2 id="图像合成"><a href="#图像合成" class="header-anchor">#</a> 图像合成</h2> <p>globalCompositeOperation 设置要在绘制新形状时应用的合成操作的类型，其中type是用于标识要使用的合成或混合模式操作的字符串。</p> <div class="language-js extra-class"><pre class="language-js"><code>context<span class="token punctuation">.</span>globalCompositeOperation <span class="token operator">=</span> <span class="token string">&quot;xor&quot;</span><span class="token punctuation">;</span>

context<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">&quot;blue&quot;</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

context<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">&quot;red&quot;</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="图像裁剪"><a href="#图像裁剪" class="header-anchor">#</a> 图像裁剪</h2> <p>裁剪是对画布进行的，裁切后的画布不能恢复到原来的大小,需要用 save(), restore()</p> <div class="language-js extra-class"><pre class="language-js"><code>context<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// 画出一道圆弧路径</span>
context<span class="token punctuation">.</span><span class="token function">arc</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">75</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">clip</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 对裁剪后的路径进行填充</span>
context<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">restore</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div><h2 id="绘制图像"><a href="#绘制图像" class="header-anchor">#</a> 绘制图像</h2> <p>drawImage()是一个很关键的方法，它可以引入图像、画布、视频，并对其进行缩放或裁剪。</p> <p>三参数: void ctx.drawImage(image, dx, dy) 五参数: void ctx.drawImage(image, dx, dy, dWidth, dHeight) 全部餐宿: void ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)</p> <table><thead><tr><th>参数</th> <th>描述</th></tr></thead> <tbody><tr><td>img</td> <td>规定要使用的图像、画布或视频。</td></tr> <tr><td>sx</td> <td>可选。开始剪切的 x 坐标位置。</td></tr> <tr><td>sy</td> <td>可选。开始剪切的 y 坐标位置。</td></tr> <tr><td>swidth</td> <td>可选。被剪切图像的宽度。</td></tr> <tr><td>sheight</td> <td>可选。被剪切图像的高度。</td></tr> <tr><td>x</td> <td>在画布上放置图像的 x 坐标位置。</td></tr> <tr><td>y</td> <td>在画布上放置图像的 y 坐标位置。</td></tr> <tr><td>width</td> <td>可选。要使用的图像的宽度。（伸展或缩小图像）</td></tr> <tr><td>height</td> <td>可选。要使用的图像的高度。（伸展或缩小图像）</td></tr></tbody></table> <h2 id="绘制路径方向"><a href="#绘制路径方向" class="header-anchor">#</a> 绘制路径方向</h2> <p>绘制同心圆</p> <div class="language-js extra-class"><pre class="language-js"><code>context<span class="token punctuation">.</span><span class="token function">arc</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">arc</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">60</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token string">&quot;red&quot;</span><span class="token punctuation">;</span>
context<span class="token punctuation">.</span><span class="token function">fill</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="橡皮擦"><a href="#橡皮擦" class="header-anchor">#</a> 橡皮擦</h2> <p>这个方法通过把像素设置为透明以达到擦除一个矩形区域的目的。</p> <p>如果没有依照 绘制路径 的步骤，使用 clearRect() 会导致意想之外的结果。请确保在调用 clearRect()之后绘制新内容前调用beginPath() 。</p> <p>清空画布</p> <div class="language-js extra-class"><pre class="language-js"><code>ctx<span class="token punctuation">.</span><span class="token function">clearRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> canvas<span class="token punctuation">.</span>width<span class="token punctuation">,</span> canvas<span class="token punctuation">.</span>height<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">10/8/2023, 3:13:07 PM</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#线条样式和基于状态绘制" class="sidebar-link reco-side-线条样式和基于状态绘制" data-v-cb1513f6>线条样式和基于状态绘制</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#快速生成矩形" class="sidebar-link reco-side-快速生成矩形" data-v-cb1513f6>快速生成矩形</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#线条属性" class="sidebar-link reco-side-线条属性" data-v-cb1513f6>线条属性</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#填充色和渐变" class="sidebar-link reco-side-填充色和渐变" data-v-cb1513f6>填充色和渐变</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#填充图案" class="sidebar-link reco-side-填充图案" data-v-cb1513f6>填充图案</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#绘制复杂路径图形" class="sidebar-link reco-side-绘制复杂路径图形" data-v-cb1513f6>绘制复杂路径图形</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#贝塞尔曲线" class="sidebar-link reco-side-贝塞尔曲线" data-v-cb1513f6>贝塞尔曲线</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#保存状态" class="sidebar-link reco-side-保存状态" data-v-cb1513f6>保存状态</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#平移变换" class="sidebar-link reco-side-平移变换" data-v-cb1513f6>平移变换</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#矩阵变换" class="sidebar-link reco-side-矩阵变换" data-v-cb1513f6>矩阵变换</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#文本显示与渲染" class="sidebar-link reco-side-文本显示与渲染" data-v-cb1513f6>文本显示与渲染</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#阴影效果" class="sidebar-link reco-side-阴影效果" data-v-cb1513f6>阴影效果</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#全局透明" class="sidebar-link reco-side-全局透明" data-v-cb1513f6>全局透明</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#图像合成" class="sidebar-link reco-side-图像合成" data-v-cb1513f6>图像合成</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#图像裁剪" class="sidebar-link reco-side-图像裁剪" data-v-cb1513f6>图像裁剪</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#绘制图像" class="sidebar-link reco-side-绘制图像" data-v-cb1513f6>绘制图像</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#绘制路径方向" class="sidebar-link reco-side-绘制路径方向" data-v-cb1513f6>绘制路径方向</a></li><li class="level-2" data-v-cb1513f6><a href="/sjj-blog/blogs/javascript/canvasapi.html#橡皮擦" class="sidebar-link reco-side-橡皮擦" data-v-cb1513f6>橡皮擦</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/sjj-blog/assets/js/app.ab5bad9b.js" defer></script><script src="/sjj-blog/assets/js/3.136f62ef.js" defer></script><script src="/sjj-blog/assets/js/1.a27fba8e.js" defer></script><script src="/sjj-blog/assets/js/13.ace41b19.js" defer></script>
  </body>
</html>
